class Doll extends eui.Component{//新建一个Doll类，继承于eui。component类。
	private giftImage:eui.Image;//新建一个全局变量giftimage为eui。image类型

	public constructor() {
		super();
		this.skinName = "resource/ui/dollSkin.exml";
	}

	private _gid :number = 0;
	public setGid(id: number){
		this._gid = id;
	}

	public getGid():number{
		return this._gid;
	}

	private updataDisplay(){
		if(this.giftImage && this._gid){
			this.giftImage.source = "gift_"+this._gid;
		}
	}

	protected childrenCreated(): void {
		this.giftImage && this.updataDisplay();
	}
	
}

class Belt extends eui.Group{
	private spacing:number = 0;
	public constructor(w:number, s:number = 210){
		super();
		this.spacing = s;
		this.width = w;
		this.reset();
	}

	private dolls:Doll[] = [];

	private createDoll():Doll{
		var d = new Doll();
		return d;
	}

	public reset(){
		var cnt = Math.ceil(this.width / this.spacing) + 1;
		this.removeChildren();
		for(var i = 0; i < cnt; i ++){
			var d = this.createDoll();
			d.anchorOffsetX = d.width / 2;
			d.anchorOffsetY = d.height;
			d.x = this.width - this.spacing * i;
			d.setGid(Math.floor(Math.random()*2) +1);
			this.addChild(d);
			this.dolls.push(d);
		}
	}

	public doMove(x:number){
		var rmFirst = false;
		for(var i in this.dolls){
			var d = this.dolls[i];
			d.x += x;
			if(d.x > this.width + this.spacing / 2){
				rmFirst = true;
			}
		}
		if(rmFirst){
            var rd = this.dolls.shift();
			rd.x = this.dolls[this.dolls.length - 1].x - this.spacing;
			this.dolls.push(rd);
		}
		
	}

	public check():Doll{
		for(var i in this.dolls){
			var d = this.dolls[i];
			if (Math.abs(d.x - this.width / 2) < 60){
				this.dolls.splice(parseInt(i), 1);
				this.removeChild(d);
				return d;
			}
		}
		return null
	}

	public dollBack(rd: Doll){
		rd.horizontalCenter = NaN;
		rd.bottom = NaN;
		rd.y = 0;
		rd.anchorOffsetX = rd.width / 2;
		rd.anchorOffsetY = rd.height;
		rd.x = this.dolls[this.dolls.length - 1].x - this.spacing;
		this.addChild(rd);
		this.dolls.push(rd);
	}
}


class GameScene extends eui.Component {

	private frontMvGroup: eui.Group;
	private bkedGroup: eui.Group;
	private jzGroup:eui.Group;
	private jzr:eui.Image;
	private jzl:eui.Image;
	private downImage:eui.Image;
	private actionBtn: eui.Button;
	public constructor() {
		super();
		this.skinName = "resource/ui/game.exml";
		this.addEventListener(egret.Event.ADDED_TO_STAGE, this.added_to_stage, this);
	}

	private added_to_stage(){
		this.addEventListener(egret.Event.ENTER_FRAME, this.onEntryFrame, this);
	}


	private backendBelt:Belt = null;
	private frontBelt:Belt = null;

	protected childrenCreated(): void {
		if(this.frontMvGroup){
			var belt = new Belt(640);
			belt.bottom = 0;
			this.frontMvGroup.addChild(belt);
			this.frontBelt = belt;
		}
		if(this.bkedGroup){
			var belt = new Belt(1000);
			belt.bottom = 0;
			belt.scaleX = -0.6
			belt.scaleY = 0.6
			belt.horizontalCenter = 0;
			this.bkedGroup.addChild(belt);
			this.backendBelt = belt;
		}

		if(this.actionBtn){
			this.actionBtn.addEventListener(egret.TouchEvent.TOUCH_TAP, this.clip, this);
		}
	}

	private isCliping:boolean = false
	private clip(){
		this.actionBtn.enabled = false;
		this.isCliping = true;
		this.jzl.rotation = 30;
		this.jzr.rotation = -30;
		egret.Tween.get(this.jzGroup).to({height:532}, 1000).call(this.clipEnd, this);
	}

	private clipEnd(){
		var d = this.frontBelt.check();
		//var b=this.chaichan;
		this.jzl.rotation = this.jzr.rotation = 0;
		if(d){
			d.horizontalCenter = 0;
			d.bottom = -d.height;
			this.jzGroup.addChild(d);
		}
		if(Math.random() > 0.5 || !d){
			egret.Tween.get(this.jzGroup).to({height:120}, 1000).call(function(){
						this.actionBtn.enabled = true;
						if(d){
							this.jzGroup.removeChild(d);
							this.frontBelt.dollBack(d);
							alert(d.getGid() == 1? "得到Q币":"得到金豆");
							//b+=10;
							//console.log({b})
						}
					}, this);
		}else{
		 	var l = Math.random() * 200 + 180;
			var t = 1000 * l / 412;
			egret.Tween.get(this.jzGroup).to({height:l}, t).call(function(){
				this.jzl.rotation = 30;
				this.jzr.rotation = -30;
				this.downImage.source = "sprite-bg-index_json.bg3"
				if(d)
				egret.Tween.get(d).to({bottom: -626}, 1000 - t)
			}, this).to({height:120}, 1000 - t).call(function(){
				this.actionBtn.enabled = true;
				if(d){
					egret.Tween.removeTweens(d);
					this.jzGroup.removeChild(d);
					this.frontBelt.dollBack(d);
				}
				this.jzl.rotation = this.jzr.rotation = 0;
				this.downImage.source = "sprite-bg-index_json.bg2"
			}, this);
		}
		
	}

	private onEntryFrame(){
		this.frontBelt.doMove(3);
		this.backendBelt.doMove(3);
	}
}